#include <stdio.h>
#include <limits.h>  // 用于INT_MAX

#define MAX_VERTICES 50  // 最大顶点数

typedef struct {
    int num_vertices;  // 图的顶点数
    int adjacency_matrix[MAX_VERTICES][MAX_VERTICES];  // 邻接矩阵
} WeightedGraph;

// 初始化图
void initialize_graph(WeightedGraph *graph, int num_vertices) {
    graph->num_vertices = num_vertices;
    for (int i = 0; i < num_vertices; i++) {
        for (int j = 0; j < num_vertices; j++) {
            if (i == j) {
                graph->adjacency_matrix[i][j] = 0;  // 顶点自身的权重为0
            } else {
                graph->adjacency_matrix[i][j] = INT_MAX;  // 表示无连接
            }
        }
    }
}

// 添加边
void add_edge(WeightedGraph *graph, int u, int v, int weight) {
    if (u >= 0 && u < graph->num_vertices && v >= 0 && v < graph->num_vertices) {
        graph->adjacency_matrix[u][v] = weight;
        graph->adjacency_matrix[v][u] = weight;  // 因为是无向图
    }
}

// 删除边
void remove_edge(WeightedGraph *graph, int u, int v) {
    if (u >= 0 && u < graph->num_vertices && v >= 0 && v < graph->num_vertices) {
        graph->adjacency_matrix[u][v] = INT_MAX;
        graph->adjacency_matrix[v][u] = INT_MAX;
    }
}

// 打印邻接矩阵
void print_graph(WeightedGraph *graph) {
    for (int i = 0; i < graph->num_vertices; i++) {
        for (int j = 0; j < graph->num_vertices; j++) {
            if (graph->adjacency_matrix[i][j] == INT_MAX) {
                printf("INF ");
            } else {
                printf("%d ", graph->adjacency_matrix[i][j]);
            }
        }
        printf("\n");
    }
}

int main() {
    WeightedGraph graph;
    initialize_graph(&graph, 5); // 初始化一个有5个顶点的图
    add_edge(&graph, 0, 1, 10);
    add_edge(&graph, 0, 4, 20);
    add_edge(&graph, 1, 2, 30);
    add_edge(&graph, 1, 3, 40);
    add_edge(&graph, 1, 4, 50);
    add_edge(&graph, 2, 3, 60);
    add_edge(&graph, 3, 4, 70);

    printf("加权邻接矩阵:\n");
    print_graph(&graph);

    return 0;
}
